<template>
  <div>
    <van-nav-bar
      title="账号登录"
      left-text=""
      left-arrow
      @click-left="$router.back()"
      class=".page-nav-bar"
    />
    <van-form @submit="onSubmit">
      <van-field
        v-model="username"
        name="username"
        label="用户名"
        placeholder="用户名"
        :rules="[{ required: true, message: '请填写用户名' }]"
      />
      <van-field
        v-model="password"
        type="password"
        name="password"
        label="密码"
        placeholder="密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />
      <div style="margin: 16px">
        <van-button round block type="info" native-type="submit" class="login"
          >提交</van-button
        >
      </div>
    </van-form>
  </div>
</template>

<script>
import request from "@/utils/request";
import { Toast } from "vant";
export default {
  name: "VueWorkLogin",

  data() {
    return {
      username: "",
      password: "",
    };
  },

  mounted() {},

  methods: {
    async onSubmit(values) {
      try {
        const res = await request({
          url: "http://liufusong.top:8080/user/login",
          method: "post",
          data: values,
        });
        console.log(res);
        localStorage.setItem("token", JSON.stringify(res.data.body.token));
        Toast("登录成功!");
        this.$router.push("/layout/my");
      } catch (error) {
        console.log(error);
      }
    },
  },
};
</script>

<style lang="less" scoped>
.van-nav-bar {
  background-color: #58b67f;
  .van-nav-bar__title {
    color: white;
    background-color: #58b67f;
  }
  i {
    color: white;
  }
}
.login {
  background-color: #58b67f;
  border: none;
}
.van-icon-arrow-left:before {
  color: black;
}
</style>
